package com.ruitu.health_2.mvp.model;

import com.ruitu.arad.api.RxUtil;
import com.ruitu.arad.base.BaseModel;
import com.ruitu.health_2.dao.http.APIRetrofit;
import com.ruitu.router_module.bean.BaseResult2;
import com.ruitu.router_module.bean.PageModel2;
import com.ruitu.router_module.bean.article.Article;
import com.ruitu.router_module.bean.home.AdvertContent;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.functions.Function;

public class ArticleModel {

    public static class ArticleListM extends BaseModel {

        public Observable<BaseResult2<PageModel2<Article>>> reqArticleListByTitle(int size, int num, String title) {
            return APIRetrofit.getDefault()
                    .reqArticleListByTitle(size, num, title)
                    .compose(RxUtil.<BaseResult2<PageModel2<Article>>>subIoObMain());
        }

        public Observable<BaseResult2<PageModel2<Article>>> reqArticleListByColumn(String posId, int size, int num) {
            return APIRetrofit.getDefault()
                    .reqArticleListByTab(posId, size, num)
                    .map(new Function<BaseResult2<PageModel2<AdvertContent<Article>>>, BaseResult2<PageModel2<Article>>>() {
                        @Override
                        public BaseResult2<PageModel2<Article>> apply(BaseResult2<PageModel2<AdvertContent<Article>>> pageModel2BaseResult2) throws Exception {
                            BaseResult2<PageModel2<Article>> baseResult2 = new BaseResult2<>();
                            baseResult2.setCode(pageModel2BaseResult2.getCode());
                            baseResult2.setMessage(pageModel2BaseResult2.getMessage());
                            List<Article> readList = new ArrayList<>();
                            for (int i = 0; i < pageModel2BaseResult2.getData().getList().size(); i++) {
                                readList.add(pageModel2BaseResult2.getData().getList().get(i).getInfoObject());
                            }
                            baseResult2.getData().setList(readList);
                            return baseResult2;
                        }
                    })
                    .compose(RxUtil.<BaseResult2<PageModel2<Article>>>subIoObMain());
        }
    }
}
